package br.ufcg.les;

import java.io.Serializable;
import java.util.ArrayList;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;

public final class ContactManager extends Activity implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	public static final String TAG = "ContactManager";
	private EditText etNomeContato;
	private Button btAddcontato;
	private ListView mContactList;
	private LinearLayout listaContatos;
	private boolean mShowInvisible;
	TextView novoItem;
	private CheckBox mShowInvisibleControl;
	private ArrayList<CheckBox> listaCheckBox;
	static ArrayList<String> contactList;
	static ArrayList<String> listaContatosPConvidados;

	/**
	 * Called when the activity is first created. Responsible for initializing
	 * the UI.
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		Log.v(TAG, "Activity State: onCreate()");
		super.onCreate(savedInstanceState);
		setContentView(R.layout.contact_manager);
		listaCheckBox = new ArrayList<CheckBox>();
		listaContatos = (LinearLayout) findViewById(R.id.listaContatos);
		contactList = new ArrayList<String>();
		listaContatosPConvidados = new ArrayList<String>();
		// Obtain handles to UI objects
		btAddcontato = (Button) findViewById(R.id.addContato);
		mContactList = (ListView) findViewById(R.id.contactList);
		mShowInvisibleControl = (CheckBox) findViewById(R.id.showInvisible);
		etNomeContato = (EditText) findViewById(R.id.etNomeContato);
		// Initialize class properties
		mShowInvisible = false;
		mShowInvisibleControl.setChecked(mShowInvisible);

		// Register handler for UI elements
		btAddcontato.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				Log.d(TAG, "mAddAccountButton clicked");
				addConvidadoNaTela(etNomeContato);
				addConvidadoInBD();
				etNomeContato.setText("");

			}

			private void addConvidadoNaTela(EditText et) {
				listaContatosPConvidados.add(et.getText().toString());
				Context context = getApplication();
				novoItem = new CheckBox(context);
				novoItem.setText(et.getText().toString());
				GuestList.listaVIew.add((CheckBox) novoItem);
				GuestList.llListaConvidados.addView(novoItem);
			}
		});

		mShowInvisibleControl
				.setOnCheckedChangeListener(new OnCheckedChangeListener() {
					public void onCheckedChanged(CompoundButton buttonView,
							boolean isChecked) {
						Log.d(TAG, "mShowInvisibleControl changed: "
								+ isChecked);
						mShowInvisible = isChecked;
						populateContactList();
					}
				});

		// Populate the contact list
		populateContactList();
	
	}

	private void addnatela() {
		for (String nome : contactList) {
			
			/*
			Context context = getApplication();
			novoItem = new CheckBox(context);
			novoItem.setText(nome.toString());
			listaCheckBox.add((CheckBox) novoItem);
			listaContatos.addView(novoItem);
			*/
		}
	
		
	}

	public void addConvidadoInBD() {
		BD banco = new BD();
		banco.insereConvidado(this, etNomeContato.getText().toString());
	}

	/**
	 * Populate the contact list based on account currently selected in the
	 * account spinner.
	 */
	private void populateContactList() {
		// Build adapter with contact entries
		Cursor cursor = getContacts();
		String[] fields = new String[] { ContactsContract.Data.DISPLAY_NAME

		};
				
		
		contactList.add(ContactsContract.Data.DISPLAY_NAME);
		SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
				R.layout.contact_entry, cursor, fields,
				new int[] { R.id.contactEntryText });
	
		System.out.println(adapter.convertToString(getContacts()));
		mContactList.setAdapter(adapter);
	}
	
	

	/**
	 * Obtains the contact list for the currently selected account.
	 * 
	 * @return A cursor for for accessing the contact list.
	 */
	public Cursor getContacts() {
		// Run query
		Uri uri = ContactsContract.Contacts.CONTENT_URI;
		String[] projection = new String[] { ContactsContract.Contacts._ID,
				ContactsContract.Contacts.DISPLAY_NAME };
		String selection = ContactsContract.Contacts.IN_VISIBLE_GROUP + " = '"
				+ (mShowInvisible ? "0" : "1") + "'";
		String[] selectionArgs = null;
		String sortOrder = ContactsContract.Contacts.DISPLAY_NAME
				+ " COLLATE LOCALIZED ASC";
		
	
		
		return managedQuery(uri, projection, selection, selectionArgs,
				sortOrder);
	}

	public void onclickPesquisar(View v) {
		Intent i = new Intent();
		i.setClass(this, ContactLocation.class);
		startActivity(i);
	}

	public ListView getmContactList() {
		return mContactList;
	}
}